package cevent.source.cloudcenter.system.controller.admin;

import cevent.source.cloudcenter.server.dto.PageDto;
import cevent.source.cloudcenter.server.dto.ResourcesDto;
import cevent.source.cloudcenter.server.dto.ResponseDataDto;
import cevent.source.cloudcenter.server.service.ResourcesService;
import cevent.source.cloudcenter.server.util.ValidatorUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author cevent
 * @description 权限资源管理表控制层
 */
@RestController
@RequestMapping("/admin/resources")
public class ResourcesController {
    private static final Logger LOG = LoggerFactory.getLogger(ResourcesController.class);
    //AOP日志环绕
    public static final String BUSINESS_NAME="权限资源管理表";
    @Resource
    private ResourcesService resourcesService;

    //1.dto-resources获取
    @RequestMapping("/dtoList")
    public List<ResourcesDto> getDtoList() {
        return resourcesService.resourcesDtoList();
    }


    /*2.前端分页设置，后端pageInfo处理
    前端传参方式一：表单方式--》formData 前端：直接传对象   后端参数：(Dto对象)
    前端传参方式二：流的方式--》data{param1:value1,param2:vlaue2}  后端参数：(@RequestBody Dto dto对象)
     */
    @PostMapping("/pageList")
    public ResponseDataDto list(@RequestBody PageDto pageDto) {
        LOG.info("查看controller的pageDto：{}", pageDto);
        ResponseDataDto responseData = new ResponseDataDto();
        resourcesService.getResourcesPageDtoList(pageDto);
        responseData.setResponseData(pageDto);
        return responseData;
    }

    /**
     * 返回tree数据结构的菜单
     * @param
     * @return
     */
    @RequestMapping("/dtoTree")
    public ResponseDataDto dtoTree(){
        ResponseDataDto responseData=new ResponseDataDto();
        List<ResourcesDto> resourcesDtos=resourcesService.getResourcesTree();
        responseData.setResponseData(resourcesDtos);
        return responseData;
    }

    /**
     * 根据unid获取资源信息
     */
    @RequestMapping("/info/{uniId}")
    public ResponseDataDto getResourcesInfo(@PathVariable("uniId") String uniId){
        ResponseDataDto responseData=new ResponseDataDto();
        ResourcesDto resourcesDto=resourcesService.getInfo(uniId);
        responseData.setResponseData(resourcesDto);
        return responseData;
    }

    //3.批量保存json
    @PostMapping("/saveResources")
    public ResponseDataDto saveResources(@RequestBody String json) {
        LOG.info("传入的json：{}", json);
        ValidatorUtil.requiredEmpty(json, "资源内容");
        ResponseDataDto responseData = new ResponseDataDto();
        ResourcesDto resourcesDto= resourcesService.saveJson(json);
        responseData.setResponseData(resourcesDto);
        return responseData;
    }

    //4.保存resources对象
    @PostMapping("/save")
    public ResponseDataDto save(@RequestBody ResourcesDto resourcesDto){
        ResponseDataDto responseData=new ResponseDataDto();
        LOG.info("传入的资源对象：{}",resourcesDto);
        ValidatorUtil.requiredEmpty(resourcesDto.getName(), "名称");
        ValidatorUtil.requiredLength(resourcesDto.getName(), "名称", 3, 255);
        resourcesService.save(resourcesDto);
        responseData.setResponseData(resourcesDto);
        return responseData;
    }

    /**
     * 指定请求的格式为Delete
     * 4.删除模块，如果为多个参数，就定义多个/{param}/{param}
     */
    @DeleteMapping("/del/{uniId}")
    public ResponseDataDto del(@PathVariable String uniId) {
        LOG.info("传入的resources uniId：{}", uniId);
        ResponseDataDto responseData = new ResponseDataDto();
        resourcesService.delete(uniId);
        return responseData;
    }

    //获取后端parentID集合，算出一级分类id
    @RequestMapping("/getParentId")
    public ResponseDataDto getParentId(){
        ResponseDataDto responseData=new ResponseDataDto();
        String pId= resourcesService.getParentId();
        responseData.setResponseData(pId);
        return responseData;
    }

    //拖拽批量保存
    @RequestMapping("/update/level")
    public ResponseDataDto updateLevelBatch(@RequestBody ResourcesDto[] resourcesDtos){
        ResponseDataDto responseData=new ResponseDataDto();
        responseData.setResponseData(resourcesService.updateLevelBatchByUnid(resourcesDtos));
        return responseData;
    }

    //批量删除
    @PostMapping("/delBatch")
    public ResponseDataDto delBatch(@RequestBody String [] uniIds){
        ResponseDataDto responseData=new ResponseDataDto();
        responseData.setResponseData(resourcesService.delBatchResources(uniIds));
        return responseData;
    }
}
